﻿
@{
    ViewBag.Title = "尚安全";
    Layout = "~/Views/Shared/_LayoutMobileLogin.cshtml";
}
@section css {
    <style>
        .ui-page-login,
        body {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }

        .mui-content {
            height: 100%;
        }

        .area {
            margin: 20px auto 0px auto;
        }

        .mui-input-group {
            margin-top: 10px;
        }

            .mui-input-group:first-child {
                margin-top: 20px;
            }

            .mui-input-group label {
                width: 22%;
            }

        .mui-input-row label ~ input,
        .mui-input-row label ~ select,
        .mui-input-row label ~ textarea {
            width: 78%;
        }

        .mui-checkbox input[type=checkbox],
        .mui-radio input[type=radio] {
            top: 6px;
        }

        .mui-content-padded {
            margin-top: 25px;
        }

        .mui-btn {
            padding: 10px;
        }

        .link-area {
            display: block;
            margin-top: 25px;
            text-align: center;
        }

        .spliter {
            color: #bbb;
            padding: 0px 8px;
        }

        .oauth-area {
            position: absolute;
            bottom: 20px;
            left: 0px;
            text-align: center;
            width: 100%;
            padding: 0px;
            margin: 0px;
        }

            .oauth-area .oauth-btn {
                display: inline-block;
                width: 50px;
                height: 50px;
                background-size: 30px 30px;
                background-position: center center;
                background-repeat: no-repeat;
                margin: 0px 20px;
                /*-webkit-filter: grayscale(100%); */
                border: solid 1px #ddd;
                border-radius: 25px;
            }

                .oauth-area .oauth-btn:active {
                    border: solid 1px #aaa;
                }

                .oauth-area .oauth-btn.disabled {
                    background-color: #ddd;
                }
    </style>
}

<header class="mui-bar mui-bar-nav">
    <h1 class="mui-title" style="color:white;">登录</h1>
</header>
<div class="mui-content" ng-cloak ng-controller="LoginController" ng-init="code='@(Request.QueryString["code"].IsEmpty() ? "" : Request.QueryString["code"])';LanxinSSO();">
    <div ng-show="!SSOLoading">
        <form id='login-form' class="mui-input-group">
            <div class="mui-input-row">
                <label>账号</label>
                <input id='account' type="text" class="mui-input-clear mui-input" ng-model="UserName" placeholder="请输入账号">
            </div>
            <div class="mui-input-row">
                <label>密码</label>
                <input id='password' type="password" class="mui-input-clear mui-input" ng-model="Password" placeholder="请输入密码">
            </div>
        </form>
        <form class="mui-input-group">
            <ul class="mui-table-view mui-table-view-chevron">
                <li class="mui-table-view-cell">
                    自动登录
                    <div id="autoLogin" class="mui-switch">
                        <div class="mui-switch-handle"></div>
                    </div>
                </li>
            </ul>
        </form>
        <div class="mui-content-padded">
            <button id='login' type="button" ng-click="UserLogin(UserName,Password);" class="mui-btn mui-btn-block mui-btn-primary">登录</button>
            <div class="link-area">
                <a id='reg'>注册账号</a> <span class="spliter">|</span> <a id='forgetPassword'>忘记密码</a>
            </div>
        </div>
    </div>
    <div ng-show="SSOLoading">
        正在验证用户信息...
    </div>
    <div class="mui-content-padded oauth-area">

    </div>
</div>
@section js{
    <script>
        mui.init({
            swipeBack: true //启用右滑关闭功能
        });
        app.controller('LoginController', function ($scope, $http) {
            $scope.LanxinSSO = function () {
                return false;
                $scope.SSOLoading = true;

                if ($scope.code == "") {
                    var backurl = encodeURIComponent("http://comac-saq.e.lanxin.cn/account/mlogin");
                    var timestamp = Date.parse(new Date());
                    location.href = "https://comacplus.e.lanxin.cn:10443/user/passport/oauth2/authorize?appid=3760384-1736704&response_type=code&scope=basic_userinfor&state=" + timestamp + "&redirect_uri=" + backurl;
                }
                else {
                    //get apptoken
                    $http.get("../Account/GetAppToken")
                        .success(function (ret) {
                            if (ret.Code == 100) {
                                $scope.AppToken = ret.Data;
                                //get usertoken
                                //1.从cookie中查找是否有usertoken
                                var utoken = getCookie("usertoken");
                                //2.已有，直接使用cookie
                                if (utoken) {
                                    $http.get("../Account/LanxinLogin?usertoken=" + utoken)
                                        .success(function (ret3) {
                                            if (ret3.Code == 100) {
                                                mui.toast("单点登陆验证通过，正在跳转");
                                                location.href = "../Home/MenuBar";
                                            }
                                            else {
                                                mui.toast("单点登陆验证失败");
                                            }
                                        });
                                }
                                else {
                                    //重新获取usertoken
                                    $http.get("../Account/GetUserToken?code=" + $scope.code)
                                        .success(function (ret2) {
                                            if (ret2.Code == 100) {
                                                $scope.UserToken = ret2.Data;
                                                setCookie("usertoken", ret2.Data, 7000);
                                                //get userinfo
                                                $http.get("../Account/LanxinLogin?usertoken=" + $scope.UserToken)
                                                    .success(function (ret3) {
                                                        if (ret3.Code == 100) {
                                                            if (ret3.Data == false) {
                                                                $http({
                                                                    method: 'POST',
                                                                    url: '../Account/CreateMyTask'
                                                                })
                                                                mui.toast("单点登陆验证通过，正在跳转");
                                                                location.href = "../Home/MenuBar";
                                                            }
                                                            else {
                                                                mui.toast('验证通过，正在跳转！');
                                                                window.location.href = "../Home/GuestMenu";
                                                            }
                                                        }
                                                        else {
                                                            mui.toast("单点登陆验证失败");
                                                        }
                                                    });
                                            }
                                            else {
                                                //获取usertoken失败
                                                $scope.SSOLoading = false;
                                                mui.toast(ret2.Msg);
                                            }
                                        });
                                }


                            }
                            else {
                                //获取apptoken失败显示报错信息
                                $scope.SSOLoading = false;
                                mui.toast(ret.Msg);
                               
                            }
                        });
                }

            }



            $scope.UserLogin = function (user, psd, type) {
                $http({
                    method: 'POST',
                    url: '../Account/MCheckLogin',
                    data: { 'userName': user, 'password': psd, 'type': type }
                })
                    .success(function (ret) {
                        if (ret.Code == 200) {
                            if (ret.Data == false || ret.Data == null) {
                                $http({
                                    method: 'POST',
                                    url: '../Account/CreateMyTask'
                                })
                                mui.toast('验证通过，正在跳转！');
                                window.location.href = "../Home/MenuBar";
                            }
                            else {
                                mui.toast('验证通过，正在跳转！');
                                window.location.href = "../Home/GuestMenu";
                            }
                        }
                        else {
                            mui.toast(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {

                    });
            }
        });

        function setCookie(name, value, seconds) {
            seconds = seconds || 0; //seconds有值就直接赋值，没有为0，这个根php不一样。
            var expires = "";
            if (seconds != 0) { //设置cookie生存时间
                var date = new Date();
                date.setTime(date.getTime() + (seconds * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            document.cookie = name + "=" + escape(value) + expires + "; path=/"; //转码并赋值
        }

        function getCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';'); //把cookie分割成组
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i]; //取得字符串
                while (c.charAt(0) == ' ') { //判断一下字符串有没有前导空格
                    c = c.substring(1, c.length); //有的话，从第二位开始取
                }
                if (c.indexOf(nameEQ) == 0) { //如果含有我们要的name
                    return unescape(c.substring(nameEQ.length, c.length)); //解码并截取我们要值
                }
            }
            return false;
        }
    </script>
}
